package com.lancoo.znbkxx.teacher.ui.fragment

import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.lancoo.znbkxx.teacher.utils.DensityUtils

/**
 * Created by wealive on 2021/4/14.
 *
 *
 */
class PageTranformWithAlpha : ViewPager2.PageTransformer {
    override fun transformPage(page: View, position: Float) {
        page.apply {
            val offset = DensityUtils.dp2px(page.context, 10f)
            val scaleFactor = (height - offset * position) / page.height
            when {
                position < -1 -> { // [-Infinity,-1)
                    alpha = 0f
                }
                position <= 0 -> { // [-1,0]
                    alpha = 1 + position
                    translationZ = 3f + position
                }
                position < 1 -> { // (0,1]
                    alpha = 1.6f - position
                    translationX = width * -position
                    translationY = offset * position
                    translationZ = 3f - position
                    scaleX = scaleFactor
                    scaleY = scaleFactor
                }
                else -> { // (1,+Infinity]
                    alpha = 0.6f - 0.2f * position
                    translationX = width * -position
                    translationY = offset * position
                    translationZ = -position
                    scaleX = scaleFactor
                    scaleY = scaleFactor
                }
            }
        }
    }
}